<template>
  <el-card class="box-card">
    <ul>
      <li>管理员信息</li>
      <li>
        <p>管理员Id：</p>
        <span>{{ accountInfo.id }}</span>
      </li>
      <li>
        <p>账号:</p>
        <span>{{ accountInfo.account }}</span>
      </li>
      <li>
        <p>用户组:</p>
        <span>{{ accountInfo.userGroup }}</span>
      </li>
      <li>
        <p>创建时间:</p>
        <span>{{ accountInfo.ctime }}</span>
      </li>
      <li>
        <p>管理员头像:</p>
        <span>
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:5000/users/avatar_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <el-button type="primary" @click="imgBtn">更换头像</el-button>
        </span>
      </li>
    </ul>
  </el-card>
</template>

<script>
import local from '../../utils/locl.js'
import { avataredit } from '../../api/users.js'

export default {
  data() {
    return {
      accountInfo: {},
      imageUrl: '',
      images: ''
    }
  },
  created() {
    this.accountInfo = JSON.parse(local.get('accountInfo'))
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      // console.log(res)
      this.images = res.imgUrl
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    async imgBtn() {
      const data = await avataredit({
        imgUrl: this.images
      })
      // console.log(data)
      if (data.code === 0) {
        this.$message({
          type: 'success',
          message: data.msg
        })
        location.reload()
      }
    }
  }
}
</script>

<style lang="less" scoped>
li {
  border-bottom: 1px solid #ccc;
  color: #999;
  font-size: 16px;
  display: flex;
  align-items: center;
  p {
    width: 100px;
    line-height: 50px;
  }
}
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
}
.avatar {
  width: 148px;
  height: 148px;
  display: block;
}
</style>
